﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jenvin.Library.Helper;
using Jenvin.Library.DataStruct;
using Jenvin.Library.Functional;
using Jenvin.Library.Threading;
using System.Threading;
using System.IO;
using System.Web;
namespace Test
{
    class Program
    {
        public class DownUrlTask : AbstractTask
        {
            public string Msg { get; set; }
            public override void Execute()
            {
                Thread.Sleep(1000);
                Console.WriteLine(Msg);
            }
        }
        public class CountTask : AbstractTask
        {
            public int Count { get; set; }

            public override void Execute()
            {
                if (Count == -1)
                {
                    this.Context.Exited = true;
                    Console.WriteLine(this.Context.Params);
                }
                this.Context.Params = (int)this.Context.Params + Count;
            }
        }
        static void Main(string[] args)
        {
            //FormShow();
         
            Jenvin.Library.DataStruct.Dictionary<int, int> mdic = new Jenvin.Library.DataStruct.Dictionary<int, int>();

            mdic.Add(1, 1);
            mdic.Add(4, 4);
            mdic.Add(7, 7);

            mdic.Remove(1);

            mdic.Add(10, 10);
         //   mdic.Add(10, 7);

            int hashCode= HashAlgorithms.AddHash("abc123", 7);
            int hashCode2= HashAlgorithms.RotateHash("abc123", 7);
            TestsRethrow testRethrow = new TestsRethrow();
            testRethrow.CatchException();
        
            string a = File.ReadAllText("test.txt");

            string s3 = a.ToXmlText();
            var s1 = a.ToSafeXml();
            var s2 = a.ToValidXml();
             int iter =100000;
             CodeTimer.Time("user regex", iter, () => {
                 a.ToSafeXml();
             });
             CodeTimer.Time("user StringBuilder", iter, () => {

                 a.ToValidXml();
             });

             CodeTimer.Time("cut ", iter, () =>
             {
                 a.Cut(100, "");
             });

             CodeTimer.Time("string", iter, () => {

                 if (a.Length > 100)
                 {
                     a.Substring(0, 100);
                 }
             });
             return;

            RedBlackTree<int> rbTree = new RedBlackTree<int>(Comparer<int>.Default);
            int id = 0;
            rbTree.Insert(100, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(80, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(120, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(70, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(90, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(110, DuplicatePolicy.DoNothing, out id);
            rbTree.Insert(130, DuplicatePolicy.DoNothing, out id);

            rbTree.Insert(1, DuplicatePolicy.DoNothing, out id);

            rbTree.Print();
            BTreeSet<int> tree = new BTreeSet<int>();
            tree.Add(100);
            tree.Add(80);
            tree.Add(120);
            tree.Add(70);
            tree.Add(90);
            tree.Add(110);
            tree.Add(130);
            int[] arry = new int[tree.Count];
            tree.CopyTo(arry, 0);
            foreach (var s in tree)
            {
                Console.Write(s);
                Console.Write(" ");
            }

            Console.WriteLine();
            Console.WriteLine(tree.InOrderTraverse());
            Console.WriteLine(tree.TraverseLayer());
            Console.WriteLine(tree.TraverseGraph());
            tree.Remove(110);
            tree.Remove(120);
            tree.Remove(130);

            Console.WriteLine(tree.InOrderTraverse());
            Console.WriteLine(tree.TraverseLayer());
            Console.WriteLine(tree.TraverseGraph());
            //arry = new int[tree.Count];
            //tree.CopyTo(arry, 0);
            //foreach (var s in tree)
            //{
            //    Console.Write(s);
            //    Console.Write(" ");
            //}
            //tree.Clear();
            //for (int i=0; i < 10000; i++)
            //{
            //    tree.Add(i);
            //}
            //foreach (int i in tree)
            //{
            //    Console.Write(i);
            //    Console.Write(" ");
            //}
            //arry = new int[tree.Count];
            //tree.CopyTo(arry, 0);
            Console.Read();
            //var sum1 = File.ReadAllLines(@"C:\Documents and Settings\jenvin\桌面\1.txt")
            //    .Select(x => double.Parse(x.Trim()))
            //    .Sum();

            //var sum2 = File.ReadAllLines(@"C:\Documents and Settings\jenvin\桌面\2.txt")
            //     .Select(x => double.Parse(x.Trim()))
            //    .Sum();

            //var sum3 = File.ReadAllLines(@"C:\Documents and Settings\jenvin\桌面\3.txt")
            //    .Select(x => double.Parse(x.Trim()))
            //    .Sum();

            //var sum = sum1 + sum2 + sum3;
            //var day = (DateTime.Now - new DateTime(2011, 3, 7)).Days;

            //var day1 = 20;//周未 3.17 - 5.30
            //var day2 = day - day1;

            //var per = sum / day;

            //var total1 = per * day1;
            //var total2 = per * day2;

            //var sister = total1 / 3 + total2 / 5;
            //var my = total1 / 3 + (total2 / 5) * 2;

            //var sistermoney = sum1 - sister;
            //var wifemoney = sum2 - my;
            //var mymoney = sum3 - my;

            //return;

            //TaskContext context = new TaskContext();

            //TaskActor actor = new TaskActor(context);
            //actor.Post(new DownUrlTask() { Msg = "1" });
            //actor.Post(new DownUrlTask() { Msg = "2" });
            //actor.Post(new DownUrlTask() { Msg = "3" });
            //actor.Post(new ExitTask());

            //TaskContext context1 = new TaskContext();
            //TaskActor actor1 = new TaskActor(context1);
            //actor1.Post(new DownUrlTask() { Msg = "4" });
            //actor1.Post(new DownUrlTask() { Msg = "5" });
            //actor1.Post(new DownUrlTask() { Msg = "6" });
            //actor1.Post(new ExitTask());

            //context.WaitForExit();
            //context1.WaitForExit();

            //TaskContext context2 = new TaskContext();
            //context2.Params = 0;
            //TaskActor actor2 = new TaskActor(context2);
            //for (int i = 0; i < 10000; i++)
            //{
            //    actor2.Post(new CountTask() { Count = i });
            //}
            //actor2.Post(new CountTask() { Count = -1 });
            //context2.WaitForExit();

            ////Thread.Sleep(200);
            //return;
            // Console.Read();


            PriorityQueue<int> queue = new PriorityQueue<int>(10);


            Random random = new Random(Guid.NewGuid().GetHashCode());

            for (int i = 0; i < 100; i++)
            {
                queue.Push(random.Next(0, 1000));
            }

            for (int i = 0; i < 10; i++)
            {
                Console.Write(queue.Pop() + " ");
            }
            Console.WriteLine();
            Console.WriteLine("////////////////////////////////////////////////");

            PriorityQueue<CacheEntry> queue2 =
                new PriorityQueue<CacheEntry>(10, (x, y) =>
                {
                    return x.acceccCount - y.acceccCount;
                });


            CacheEntry entry1 = new CacheEntry();
            entry1.acceccCount = 1;
            CacheEntry entry2 = new CacheEntry();
            entry2.acceccCount = 2;
            entry2.lastAccessTime = DateTime.Now;

            CacheEntry entry3 = new CacheEntry();
            entry3.acceccCount = 2;
            entry3.lastAccessTime = DateTime.Now.AddDays(-1);

            CacheEntry entry4 = new CacheEntry();
            entry4.acceccCount = 2;
            entry4.lastAccessTime = DateTime.Now.AddDays(-1);
            //IntWraper intWrapper = queue2.Top();
            queue2.Push(entry1);
            queue2.Push(entry2);
            queue2.Push(entry3);
            queue2.Push(entry4);

            for (int i = queue2.Size(); i > 0; i--)
            {
                CacheEntry c = queue2.Pop();
                Console.WriteLine(c.acceccCount + " " + c.lastAccessTime);
            }


            Console.WriteLine();
            Console.Read();
        }

        public static void FormShow()
        {
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            form.Text = "人碳酸没";
            form.Show();

        

        }
    }
}
